//
//  TWCommentView.m
//  TWHomework-WeChat
//
//  Created by wzb on 2019/11/16.
//  Copyright © 2019 WZB. All rights reserved.
//

#import "TWCommentView.h"
#import "Define.h"
#import "TWTweet.h"
#import "TWUserInfo.h"
#import <Masonry/Masonry.h>
#import "UIView+TW.h"

#define TWCommentCellID @"TWCommentCellID"

/// 富文本处理
/// @param string 传入要处理的总字符串
/// @param dealStrs 要处理的总字符串
/// @param attDics 处理参数
NSMutableAttributedString* addAttString(NSString *string, NSArray *dealStrs, NSArray *attDics)
{
    NSMutableAttributedString *attString = [[NSMutableAttributedString alloc] initWithString:string];
    for (NSInteger i = 0; i < MIN(dealStrs.count, attDics.count); i++) {
        [attString addAttributes:attDics[i] range:[string rangeOfString:dealStrs[i]]];
    }
    return attString;
}

@interface TWCommentView () <UITableViewDelegate, UITableViewDataSource>

// 背景
@property (nonatomic, strong) UIImageView *bgImgView;
// 显示评论
@property (nonatomic, strong) UITableView *commentTableView;

@end

@implementation TWCommentView

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self configUI];
    }
    return self;
}

- (void)configUI
{
    // 背景
    UIImageView *bgImgView = [[UIImageView alloc] init];
    [self addSubview:bgImgView];
    self.bgImgView = bgImgView;
    bgImgView.image = [[UIImage imageNamed:@"comment_bg"] stretchableImageWithLeftCapWidth:40 topCapHeight:30];
    [bgImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.offset(0);
    }];

    // 显示评论
    UITableView *commentTableView = [[UITableView alloc] init];
    [self addSubview:commentTableView];
    self.commentTableView = commentTableView;
    commentTableView.delegate = self;
    commentTableView.dataSource = self;
//    commentTableView.rowHeight = UITableViewAutomaticDimension;
//    commentTableView.estimatedRowHeight = 40;
    commentTableView.tableFooterView = [UIView new];
    commentTableView.backgroundColor = [UIColor clearColor];
    commentTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [commentTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.offset(0);
        make.top.offset(10);
    }];
}

- (void)setComments:(NSArray<TWTweetComment *> *)comments
{
    _comments = comments;
    CGFloat totalHeight = 0;
    for (TWTweetComment *comment in comments) {
        totalHeight += [comment cellHeightWithPreferWidth:self.width];
    }
    [self.commentTableView reloadData];
    [self.commentTableView layoutIfNeeded];
    self.height = totalHeight + 15;
}

#pragma mark - UITableViewDelegate, UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.comments.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    TWCommentCell *cell = [tableView dequeueReusableCellWithIdentifier:TWCommentCellID];
    if (!cell) {
        cell = [[TWCommentCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:TWCommentCellID];
    }
    cell.comment = self.comments[indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    TWTweetComment *comment = self.comments[indexPath.row];
    return comment.cellHeight;
}

@end


@interface TWCommentCell ()

@property (nonatomic, strong) UILabel *contentLabel;

@end

@implementation TWCommentCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.backgroundColor = [UIColor clearColor];
        [self configUI];
    }
    return self;
}

- (void)configUI
{
    UILabel *contentLabel = [[UILabel alloc] init];
    [self.contentView addSubview:contentLabel];
    self.contentLabel = contentLabel;
    contentLabel.font = kComTextFont;
    contentLabel.textColor = [UIColor blackColor];
    contentLabel.numberOfLines = 0;
    [contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(10);
        make.right.offset(-10);
        make.top.bottom.offset(0);
    }];
}

- (void)setComment:(TWTweetComment *)comment
{
    _comment = comment;
    NSString *string = [NSString stringWithFormat:@"%@:%@", comment.sender.username, comment.content];
    self.contentLabel.attributedText = addAttString(string, @[comment.sender.username], @[ @{NSForegroundColorAttributeName : kLinkTextColor} ]);
    
}

@end
